
#coding: utf-8

import config
from  libs.util.url_encode import custom_url_encode
import libs.util.get_dwz as get_dwz

__app_id = config.app_id
___uri_template = "https://open.weixin.qq.com/connect/oauth2/authorize?" \
                  "appid="+ __app_id +"&redirect_uri=%s&response_type=code" \
                  "&scope=snsapi_base&state=%s#wechat_redirect"
___uri_template_userinfo = "https://open.weixin.qq.com/connect/oauth2/authorize?" \
                  "appid="+ __app_id +"&redirect_uri=%s&response_type=code" \
                  "&scope=snsapi_userinfo&state=%s#wechat_redirect"

#用于网页授权获取openid
def baseapi_link(callback_link, state_arg):
    #url_encoded = custom_url_encode(callback_link)
    full_url = ___uri_template % (callback_link, state_arg)
    dwz_data = get_dwz.get_dwz(full_url)
    if(dwz_data['status'] == 0):
        return dwz_data['tinyurl']
    else:
        #@TODO defalut
        return 123

#弹出授权页面，可通过openid拿到昵称、性别、所在地。并且，即使在未关注的情况下，只要用户授权，也能获取其信息
def userinfo_link(callback_link, state_arg):
    full_url =  ___uri_template_userinfo % (callback_link, state_arg)
    dwz_data = get_dwz.get_dwz(full_url)
    if(dwz_data['status'] == 0):
        return dwz_data['tinyurl']
    else:
        #@TODO defalut
        return 123
